package com.campus.dao.impl;

import com.campus.dao.ActivityDao;
import com.campus.model.Activity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author HLQ
 * @date 2024/11/16 16:33
 */
@Slf4j
@Repository
public class ActivityDaoImpl implements ActivityDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    //mapping
    private final RowMapper<Activity> activityRowMapper = new RowMapper<Activity>() {
        @Override
        public Activity mapRow(ResultSet rs, int rowNum) throws SQLException {
            Activity activity = new Activity()
                    .setId(rs.getInt("id"))
                    .setName(rs.getString("name"))
                    .setCategory(rs.getString("category"))
                    .setTitle(rs.getString("title"))
                    .setCreator(rs.getString("creator"))
                    .setAbstracts(rs.getString("abstract"))
                    .setDetailedDesc(rs.getString("detailed_desc"))
                    .setStatus(rs.getInt("status"))
                    .setCreateTime(rs.getTime("create_time"))
                    .setArchiveTime(rs.getTime("archive_time"))
                    .setPicUrls(rs.getString("pic_urls"))
                    .setStartTime(rs.getTime("start_time"))
                    .setEndTime(rs.getTime("end_time"));
            return activity;
        }
    };

    @Override
    public int addActivity(Activity activity) {
        String sql = "INSERT INTO t_manager_activity " +
                "(name, category, title, creator, abstract, detailed_desc, status, create_time, " +
                "archive_time, pic_urls, start_time, end_time) " +
                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] args = new Object[]{
                activity.getName(),
                activity.getCategory(),
                activity.getTitle(),
                activity.getCreator(),
                activity.getAbstracts(),
                activity.getDetailedDesc(),
                activity.getStatus(),
                activity.getCreateTime(),
                activity.getArchiveTime(),
                activity.getPicUrls(),
                activity.getStartTime(),
                activity.getEndTime()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int deleteActivityById(int id) {
        String sql = "DELETE FROM t_manager_activity WHERE id = ?";
        return jdbcTemplate.update(sql,id);
    }

    @Override
    public int updateStatusById(int status, int id) {
        String sql = "UPDATE t_manager_activity SET status = ? WHERE id = ?";
        return jdbcTemplate.update(sql, status,id);
    }
    @Override
    public int updateActivityById(Activity activity) {
        String sql = "UPDATE t_manager_activity SET " +
                "name = ?, category = ?, title = ?, creator = ?, abstract = ?, detailed_desc = ?, " +
                "status = ?, create_time = ?, archive_time = ?, pic_urls = ?, start_time = ?, end_time = ? " +
                "WHERE id = ?";
        Object[] args = new Object[]{
                activity.getName(),
                activity.getCategory(),
                activity.getTitle(),
                activity.getCreator(),
                activity.getAbstracts(),
                activity.getDetailedDesc(),
                activity.getStatus(),
                activity.getCreateTime(),
                activity.getArchiveTime(),
                activity.getPicUrls(),
                activity.getStartTime(),
                activity.getEndTime(),
                activity.getId()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public List<Activity> queryActivityById(int id) {
        String sql = "SELECT * FROM t_manager_activity WHERE id = ? ORDER BY create_time DESC";
        return jdbcTemplate.query(sql,new Object[]{id},activityRowMapper);
    }

    @Override
    public List<Activity> queryActivityByStatus(int status) {
        String sql = "SELECT * FROM t_manager_activity WHERE status = ? ORDER BY create_time DESC";
        return jdbcTemplate.query(sql,new Object[]{status},activityRowMapper);
    }
    @Override
    public List<Activity> fuzzyMatchingActivityByName(String name) {
        String sql = "SELECT * FROM t_manager_activity WHERE name LIKE ? ORDER BY create_time DESC";
        String searchName = "%" + name + "%";
        return jdbcTemplate.query(sql,new Object[]{searchName},activityRowMapper);
    }
}
